<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller\ding;

use app\api\controller\Controller;
use app\api\model\ding\Friend as FriendModel;
use app\api\model\ding\FriendCategory as CategoryModel;
use app\api\model\store\shop\Clerk as ClerkModel;
use app\api\service\User as UserService;
use app\common\service\qrcode\Friend as PosterService;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**

 * Class Cart
 * @package app\api\controller
 */
class Friend extends Controller
{
    /**
     * 购物车商品列表
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \cores\exception\BaseException
     */
    public function list(): Json
    {

        $service = new FriendModel;

        $list = $service->getList($this->request->param());
        //$url = '点击查看包间环境及预订→#小程序://订个包间/找我预订/';
        $url = Db::name('store')->value('url_name');
        return $this->renderSuccess(compact('list', 'url'));
    }
    public function categoryList()
    {
        $model = new CategoryModel;
        $list  = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    public function edit(int $shopId = 0): Json
    {
        $model = new FriendModel;

        $model->add($shopId, $this->postForm());
        return $this->renderSuccess([], '保存成功');
    }

    public function getPosterUrl(int $friendId): Json
    {
        $model = new FriendModel;

        $detail = $model->getDetail($friendId);

        $user = UserService::getCurrentLoginUser(true);

        $clerk = ClerkModel::detail(['user_id' => $user['user_id']], ['user.avatar', 'shop', 'role']);

        $Qrcode = new PosterService($detail, $clerk);
        $url    = $Qrcode->getImage();

        $detail->edit(['poster_url' => $url]);

        return $this->renderSuccess(['imageUrl' => $url]);

    }

    public function addCopyLog(int $clerkId, int $friendId): Json
    {
        $model = new FriendModel;

        $detail = $model->addView($clerkId, $friendId);

        return $this->renderSuccess([], '操作成功');
    }
}
